// ==UserScript==
// @name         获取闲鱼图片
// @namespace    http://tampermonkey.net/
// @version      2024-10-06
// @description  try to take over the world!
// @author       You
// @match        https://www.goofish.com/item?*
// @icon         http://wuweijun.fun/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
  'use strict';


  function loadImg(){
      document.querySelectorAll('.item-main-window-list--od7DK4Fm .fadeInImg--rURePiIf').forEach((item,index)=>{
          var s = item.src;
          var a = s.split('_!!0-fleamarket.jpg')[0]
          var b = a.split('/')
          var c = b[b.length-1]
          var n = document.querySelector('.desc--WLgQcGKD').innerText.substr(0,30);
          downloadIamge(`http://img.alicdn.com/bao/uploaded/i3/${c}_!!0-fleamarket.jpg`, n + '-' + index)
      })
  }

  function downloadIamge(imgsrc, name) {//下载图片地址和图片名
      let image = new Image();
      // 解决跨域 Canvas 污染问题
      image.setAttribute("crossOrigin", "anonymous");
      image.onload = function () {
          let canvas = document.createElement("canvas");
          canvas.width = image.width;
          canvas.height = image.height;
          let context = canvas.getContext("2d");
          context.drawImage(image, 0, 0, image.width, image.height);
          let url = canvas.toDataURL("image/jpg"); //得到图片的base64编码数据
          let a = document.createElement("a"); // 生成一个a元素
          let event = new MouseEvent("click"); // 创建一个单击事件
          a.download = name || "photo"; // 设置图片名称
          a.href = url; // 将生成的URL设置为a.href属性
          a.dispatchEvent(event); // 触发a的单击事件
          window.URL.revokeObjectURL(url); // 释放掉 blob 对象
      };
      image.src = imgsrc;
  }

  var oLi = document.createElement("div");
  oLi.innerHTML =`
<div style="
  position: absolute;
  top: 146px;
  left: 146px;
  background-color: #7ec1da;
  z-index: 9999;
  width: 45px;
  height: 45px;
  !i;!;
  border-radius: 50%;
  text-align: center;
  line-height: 45px;
  color: #fbefef;
  cursor: pointer;"  id="loadImgTo">图片</div>`

  var body2 = document.body;

      body2.appendChild(oLi)

      document.getElementById('loadImgTo').onclick = loadImg


      // Your code here...
  })();